package app

import (
	"github.com/gin-gonic/gin"
	"wx_build/core/msg"
	"wx_build/service"
)

type UserApi struct {
}

// CreateUser 创建用户
func (UserApi) CreateUser(g *gin.Context) {
	var dataService service.UserService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.Username == "" {
		msg.Error(g, "用户名不能为空", nil)
		return
	}
	if dataService.Password == "" {
		msg.Error(g, "密码不能为空", nil)
		return
	}
	dataService.CreateUser(g)
	return
}

// List 查询用户列表
func (UserApi) List(g *gin.Context) {
	var dataService service.UserService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.Page == 0 {
		dataService.Page = 1
	}
	if dataService.Size == 0 {
		dataService.Size = 10
	}
	dataService.List(g)
	return
}
func (UserApi) Delete(g *gin.Context) {
	var dataService service.UserService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.UserId == "" {
		msg.Success(g, "用户id不能为空")
		return
	}
	dataService.Delete(g)
	return
}
func (UserApi) Login(g *gin.Context) {
	var dataService service.UserService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.Username == "" {
		msg.Error(g, "用户名不能为空", nil)
		return
	}
	if dataService.Password == "" {
		msg.Error(g, "密码不能为空", nil)
		return
	}
	dataService.Login(g)
	return

}
func (UserApi) IsLogin(g *gin.Context) {
	msg.Success(g, "登录成功")
	return
}
func (UserApi) SetPassword(g *gin.Context) {
	var dataService service.UserService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.UserId == "" {
		msg.Error(g, "user_id不能为空", nil)
		return
	}
	if dataService.Password == "" {
		msg.Error(g, "老密码不能为空", nil)
		return
	}
	if dataService.NewPassword == "" {
		msg.Error(g, "新密码不能为空", nil)
	}
	dataService.SetPassword(g)
	return
}
